package cn.okcode.slowjson;

import cn.okcode.slowjson.internal.TypeCastUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * json对象的java定义类型
 *
 * @author pyk13
 */
public final class JsonObject extends LinkedHashMap<String, Object> implements Serializable {

    public JsonObject(int initialCapacity) {
        super(initialCapacity);
    }

    public JsonObject() {
        super(11);
    }

    public JsonObject getJsonObject(String key) {
        Object value = get(key);
        if (value instanceof JsonObject) {
            return (JsonObject) value;
        } else {
            throw new ClassCastException(key + "对应的值不是JsonObject对象");
        }
    }

    public JsonArray getJsonArray(String key) {
        Object value = get(key);
        if (value instanceof JsonArray) {
            return (JsonArray) value;
        } else {
            throw new ClassCastException(key + "对应的值不是JsonArray对象");
        }
    }

    public Boolean getBoolean(String key) {
        return TypeCastUtils.castToBoolean(get(key));
    }

    public boolean getBooleanValue(String key) {
        return TypeCastUtils.castToBoolean(get(key));
    }

    public Double getNumber(String key) {
        return TypeCastUtils.castToDouble(get(key));
    }

    public double getNumberValue(String key) {
        return TypeCastUtils.castToDouble(get(key));
    }

    public String getString(String key) {
        return TypeCastUtils.castToString(get(key));
    }

    public Integer getInteger(String key) {
        return TypeCastUtils.castToInt(get(key));
    }

    public int getIntegerValue(String key) {
        return TypeCastUtils.castToInt(get(key));
    }

    public Date getDate(String key) {
        return TypeCastUtils.castToDate(get(key));
    }

    public Short getShort(String key) {
        return TypeCastUtils.castToShort(get(key));
    }

    public short getShortValue(String key) {
        return TypeCastUtils.castToShort(get(key));
    }
}
